
const express = require(`express`)
const app = express()

const router = express.Router()
const {login,zhuce,ediltPass} =require('./api/user')
const multer = require("multer")
var bodyParse = require("body-parser");
app.use(bodyParse.urlencoded({
    extended:false // 这里是返回数据为false则是对象或者数组形式，为true则为任意数据类型
    }))
app.use(bodyParse.json())
app.use(bodyParse.urlencoded({ extended: true }))
   

let pathArr =[]
let io =0
let oi =0
let obj={
name:'',
type:'',
rmke:''
}
const {ReplyFeedbackReviewsFn,
    delFeedbackReviewsFn,
    addFeedbackReviewsFn,
    InquiryCommentFn,
    FeedbackReviewsFn,
     GuitarFn,
     uptxy,
     deltxy,
     delguitarFn,
     delFn,
     ListFn,
     SearchFn,
     addFn,
     Edilt,
     addrandomFn,
     randomList
    } =require('./api/data')
const bd = require('./bd')
app.use(express.static('static'));


 
// 自定义 multer 的 diskStorage 的存储目录与文件名
var storage = multer.diskStorage({
 destination: function (req, file, cb) {
 cb(null, './public/image')
 },
 filename: function (req, file, cb) {
     

   const indexOftp = file.originalname.indexOf('.')
 const  Format = file.originalname.slice(indexOftp,file.originalname.length)

 cb(null,`${obj.name}-${ io++}${Format}`)
 pathArr =[]
setTimeout(()=>{
 pathArr.push(`./public/image/${obj.name}-${oi++}${Format}` )
  
    // uptxy( `${obj.name}`,`${obj.name}-${io++}`,obj)
    // uptxy( pathArr)
},1000)
console.log(pathArr);
 }
})

var upload = multer({ storage: storage })
 
    /* 注册接口 */
router.post('/register', (req, res, next)=> {
    zhuce(req, res)
  });
//登录接口
router.post('/login',(req,res,next)=>{
   login(req,res)
  })
  // 修改密码接口
  router.post('/ediltpass',(erq,res,next)=>{
    ediltPass(erq,res,next)
})
   // 查询 所有数据
   router.get('/List', (req, res) => {
  ListFn(req,res,'workerinfo')

})

router.post('/beizhu',(req,res)=>{
    io = 1
    oi = 1
    weiyi = 0
    resArr =[]
   const body = req.body|| req.params
    obj.name =  body.name
    obj.mark = body.rmke
   obj.type = body.type


    res.send({ code:200 ,message: '上传成功'})
  
})
let weiyi=0
let resArr =[]
router.post('/upload',upload.any(),(req,res)=>{
   const Time= setTimeout(()=>{
    res.send({ code:200 ,message: '上传成功'})
        weiyi++
        if(weiyi ==1) uptxy(pathArr,`${obj.name}`,obj)
    
if(weiyi ===1) clearTimeout(Time)
        //    uptxy( pathArr)
    },3000)
   
    })
    	
// 查询所有 指弹
router.get('/burgeeset',(req,res) =>{
    GuitarFn(req,res,'burgeeset')

})
// router.get('/burgeeset/List',(req,res) =>{
//     GuitarListFn(req,res,'burgeeset')

// })
// router.get('/guitarSet/List',(req,res) =>{
//     GuitarListFn(req,res,'guitarSet')

// })
// 查询所有 弹唱
router.get('/guitarSet',(req,res) =>{
    GuitarFn(req,res,'guitarset')

})
// 查询 弹唱条件
router.get('/guitarSet/Search',(req,res)=>{
    SearchFn(req,res,'guitarset')
})
// 查询 指弹条件
router.get('/burgeeset/Search',(req,res)=>{
    SearchFn(req,res,'burgeeset')
})
// 删除 指弹数据
router.delete('/delburgeeset',(req,res)=>{
    delguitarFn(req,res,'burgeeset')
})
// 删除 弹唱数据
router.delete('/delguitarSet',(req,res)=>{
    delguitarFn(req,res,'guitarset')
})
// 点击数 来推荐吉他谱
router.get('/HitCount',(req,res)=>{

})
// 推荐 吉他谱
router.get('/Recommend',(req,res)=>{

})
// 查询 弹唱评论
router.get('/guitarset/inquirycomment',(req,res)=>{
    InquiryCommentFn(req,res,'guitarsetcomment')
})
// 查询 指弹评论
router.get('/burgeeset/inquirycomment',(req,res)=>{
    InquiryCommentFn(req,res,'burgeesetcomment')
})
// 反馈
router.get('/comment',(req,res)=>{
    FeedbackReviewsFn(req,res)
})
// 添加反馈
router.post('/addcomment',(req,res)=>{
    console.log(req.body.reply_comment);
    if( req.body.reply_comment ){
        ReplyFeedbackReviewsFn(req,res)
    }else{
        addFeedbackReviewsFn(req,res)
    }
//   req.body.reply_comment == 'undefined' ?addFeedbackReviewsFn(req,res) : ReplyFeedbackReviewsFn(req,res)
    
})
// 回复反馈
router.post('/replycomment',(req,res)=>{
    ReplyFeedbackReviewsFn(req,res)
})
// 删除反馈
router.delete('/delcomment',(req,res)=>{
    delFeedbackReviewsFn(req,res)
})
// 添加
router.post('/addList', (req, res) => {
//    console.log(req.body);
    addFn(req,res)

})
// 删除
router.delete('/delList', (req, res) => {
   
    delFn(req,res)

})
// 添加随机数
router.post('/addRandomList', (req, res) => {
   
    addrandomFn(req,res)

})
// 查询随机数
router.get('/RandomList', (req, res) => {
   
    randomList(req,res)

})
// 修改
router.post('/ediltList',(req,res)=>{
    Edilt(req,res)
})


module.exports = router